//
//  RootViewController.m
//  EventViewExample
//

#import "RootViewController.h"

@implementation RootViewController

@synthesize eventArray, eventStore;

- (void)refreshSampleData {
	NSDate *startDate = [NSDate date];
	
	// 오늘부터 1개월 이후의 날짜 준비
	NSDateComponents *comp = [[NSDateComponents alloc] init];
	[comp setMonth:1];
	NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
	NSDate *endDate = [cal dateByAddingComponents:comp toDate:startDate options:0];
	[comp release];
	[cal release];
	
	// 날짜 범위를 나타내는 조건 생성
	NSPredicate *predicate = [eventStore predicateForEventsWithStartDate:startDate
																 endDate:endDate
															   calendars:nil];
	NSArray *matchingEvents = [eventStore eventsMatchingPredicate:predicate];
	
	// 조건에 해당하는 이벤트를 뽑아오고, 테이블 뷰를 새로 고침한다.
	self.eventArray = [[NSArray alloc] initWithArray:matchingEvents];
	[self.tableView reloadData];
}

- (void)viewEvent:(EKEvent *)event {
	EKEventViewController *editController = [[EKEventViewController alloc] init];
	editController.event = event;
	[self.navigationController pushViewController:editController animated:YES];
	[editController release];
}

- (void)addEvent:(id)sender {
	EKEventEditViewController *addController = [[EKEventEditViewController alloc] init];
	
	addController.eventStore = self.eventStore;
	addController.editViewDelegate = self;
	
	[self presentModalViewController:addController animated:YES];
	
	[addController release];
}

- (void)viewDidLoad {
    [super viewDidLoad];

	self.title = @"Events";
	self.eventStore = [[EKEventStore alloc] init];
	
	self.navigationController.delegate = self;
	
	UIBarButtonItem *addButton = [[UIBarButtonItem alloc]
								  initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
								  target:self
								  action:@selector(addEvent:)];
	self.navigationItem.rightBarButtonItem = addButton;
	[addButton release];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.eventArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
									   reuseIdentifier:CellIdentifier] autorelease];
    }
    
	cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
	
	EKEvent *event = [self.eventArray objectAtIndex:indexPath.row];
	cell.textLabel.text = event.title;

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
	EKEvent *event = [self.eventArray objectAtIndex:indexPath.row];
	[self viewEvent:event];
}

- (void)navigationController:(UINavigationController *)navigationController
	  willShowViewController:(UIViewController *)viewController
					animated:(BOOL)animated {
	if (viewController == self) {
		[self refreshSampleData];
	}
}

- (void)eventEditViewController:(EKEventEditViewController *)controller
		  didCompleteWithAction:(EKEventEditViewAction)action {
	
	if (action == EKEventEditViewActionSaved) {
		[controller.eventStore saveEvent:controller.event span:EKSpanThisEvent error:nil];
		[self refreshSampleData];
	}
	
	[controller dismissModalViewControllerAnimated:YES];
}

- (void)dealloc {
	
	[eventArray release];
	[eventStore release];
	
    [super dealloc];
}

@end
